import tkinter as tk
def searchAndReplace():
    # 获取 原本 多文本框 text  查找内容、替换 单文本框 entry
    text = text01.get(1.0, tk.END).strip()
    search = text02.get()
    replace = text03.get()
    # 记录位置
    cur = 0
    # 记录替换次数
    total = 0
    # 记录替换位置数据
    data = []
    # 清空 多文本需要从1.0 开始删除
    text04.delete(1.0, tk.END)
    text05.delete(0, tk.END)
    text06.delete(0, tk.END)
    if text != '':
        while True:
            flag = text.find(search)
            if not flag == -1 and not search == '':
                data.append(str(cur + flag + 1))
                text = text[flag + len(search):]
                cur = cur + flag + len(search)
                total += 1
            else:
                break
        text04.insert(1.0, text01.get(1.0, tk.END).replace(search, replace))
        text05.insert(0, str(total))
        text06.insert(0,' '.join(data))
win = tk.Tk()
win.title('查找替换')
win.geometry('560x320')
# 阻止Python GUI的大小调整
win.resizable(0, 0)
# 建组件
lb01 = tk.Label(win, text='原文内容:')
text01 = tk.Text(win)
lb02 = tk.Label(win, text='查找内容:')
text02 = tk.Entry(win)
lb03 = tk.Label(win, text='替换为:')
text03 = tk.Entry(win)
lb04 = tk.Label(win, text='替换结果:')
text04 = tk.Text(win)
lb05 = tk.Label(win, text='替换次数:')
text05 = tk.Entry(win)
lb06 = tk.Label(win, text='原文中的起始位置:')
text06 = tk.Entry(win)
btn = tk.Button(win, text='全部替换', command=searchAndReplace)
# 布局
lb01.place(x=15, y=0)
text01.place(x=15, y=25, width=520, height=70)
lb02.place(x=15, y=100, )
text02.place(x=100, y=100, width=150, height=25)
lb03.place(x=310, y=100, )
text03.place(x=385, y=100, width=150, height=25)
lb04.place(x=15, y=125, )
text04.place(x=15, y=150, width=520, height=70)
lb05.place(x=15, y=230, )
text05.place(x=100, y=230, width=150, height=25)
lb06.place(x=270, y=230, )
text06.place(x=385, y=230, width=150, height=25)
btn.place(x=240, y=270)
# 接收窗体事件
win.mainloop()